import { _decorator, Component, instantiate, Node, Prefab, resources, tween, v3, Vec3 } from 'cc';
import { EPOCH, LV } from '../AshFramework/Datas/GameConstant';
import { GameManager } from './GameManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { Chariot } from './Chariot';
import { GameUtil } from './GameUtil';
import { BattleManager, CHARIOTSPEED } from './BattleManager';
const { ccclass, property } = _decorator;

const mapStartPos: Vec3 = v3(540, 0);
const mapEndPos: Vec3 = v3(2700, 0);

@ccclass('MapController')
export class MapController extends Component {

    public static Instance: MapController = null;
    @property(Node)
    Destination: Node = null;

    @property(Node)
    RoleNode: Node = null;

    private _speed: number = 100;
    private _epoch: EPOCH = EPOCH.EPOCH_1;
    private _lv: LV = LV.LV_1;

    private _totalDis: number;
    private _isStart: boolean = false;

    protected onLoad(): void {
        MapController.Instance = this;
        this.node.position = mapStartPos;
        this._epoch = GameManager.Instance.userData.epoch;
        this._lv = GameManager.Instance.userData.lv;
    }
    protected start(): void {
        tween(this.node)
            .delay(0.5)
            .to(1, { position: mapEndPos }, { easing: `linear` })
            .call(() => {
                this.gameStart();
                this._totalDis = GameUtil.getDistanceByNode(this.Destination, this.RoleNode);
                this._isStart = true;
            })
            .start();
    }
    protected update(dt: number): void {
        if (this._isStart) {
            BattleManager.Instance.updateLVProgress(this.getLVProgress());
        }

    }

    getLVProgress() {
        let progress = (this._totalDis - GameUtil.getDistanceByNode(this.Destination, this.RoleNode)) / (this._totalDis - 350);
        if (progress >= 1) {
            progress = 1
        } else if (progress <= 0) {
            progress = 0;
        }
        return progress;
    }

    gameStart() {
        // this.mapRool();
        this.schedule(this.createMonster, 3);
        this.initRole();
    }

    // mapRool() {
    //     tween(this.node)
    //         .to((mapEndPos.x - mapStartPos.x) / this._speed, { position: mapStartPos }, { easing: `linear` })
    //         .start();
    // }

    initRole() {
        this.RoleNode.active = true;
        Chariot.Instance.dirX = CHARIOTSPEED.RESUME;
    }

    createMonster() {
        ResourceUtil.LoadPrefab("Game/Monster1").then((prefab: Prefab) => {
            let monster: Node = instantiate(prefab);
            this.node.addChild(monster);
        })
        ResourceUtil.LoadPrefab("Game/Monster2").then((prefab: Prefab) => {
            let monster: Node = instantiate(prefab);
            this.node.addChild(monster);
        })
        ResourceUtil.LoadPrefab("Game/Monster3").then((prefab: Prefab) => {
            let monster: Node = instantiate(prefab);
            this.node.addChild(monster);
        })
    }

}


